<?php
class Enterprisesmodel extends CI_Model {

	function __construct()
	{
		parent::__construct();
	}

	public function init()
	{
		$dbHandle = $this->load->database('dpchandle', TRUE);
		if($dbHandle == ''){
			error_log('can not create db handle','qna');
			echo (print_r($dbHandle,true));
		}
		return $dbHandle;
	}

	//-----------LOGIN & LOGOUT----------------------------------------------

	function uploadProductPhotoCode($filename, $filetemp){
		$uploadfile = '';
		if(isset($filetemp) && !empty($filetemp)) {
			$baseName = basename($filename);
			$uploadfile = 'public/images/all/200x110/'. $baseName;
			error_log($filetemp.">>>>>>>>>>>>>".$filename);
			move_uploaded_file($filetemp, $uploadfile);
		}
	 return;
	}

	public function authenticate($post){
		$dbHandle = $this->init();
		$username=$post['username'];
		$password=$post['password'];
		$query = "SELECT * FROM dpc_user_tbl where user_username='$username' and user_password='$password' and user_status='Yes'";
		$result = mysql_query($query);
		$userArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$userArray[$i] = $row;
			$i++;
		}
		return $userArray;
	}

	//------------------USERS-------------------------------------------

	public function getAllUsers(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_user_tbl where user_status='Yes'";
		$result = mysql_query($query);
		$usersArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$usersArray[$i] = $row;
			$i++;
		}
		//error_log(print_r($usersArray));
		return $usersArray;
	}

	public function insertUser($post){
		$dbHandle = $this->init();
		$query = "insert into dpc_user_tbl(user_username, user_password, user_status) values('".$post['username']."', '".$post['password']."', 'Yes')";
		//error_log("insert user ".$query);
		$result = $dbHandle->query($query);
	}

	public function deleteUser($userId){
		$dbHandle = $this->init();
		$query = "update dpc_user_tbl set user_status='No' where user_id='$userId'";
		$result = $dbHandle->query($query);
	}

	public function updatePermissions($post){
		$dbHandle = $this->init();
		//error_log("post arrif(!isset($perms)) {
		$perms=$post['permission'];
		$perms=implode(",", $perms);
		//error_log("permissions ".print_r($perms, true));
		$query = "update dpc_user_tbl set user_permissions='$perms' where user_id='".$post['username']."'";
		//error_log("query ".$query);
		$result = $dbHandle->query($query);
	}
	
	public function getAllStores()
	{
		$dbHandle = $this->init();
		$query= "Select * from dpc_store_tbl WHERE store_status = 'Yes' ORDER BY store_name ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getStoresForCpn()
	{
		$dbHandle = $this->init();
		$query= "Select * from dpc_store_tbl WHERE store_status = 'Yes' or store_status='Process' ORDER BY store_name ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getProcessStores()
	{
		$dbHandle = $this->init();
		$query= "Select * from dpc_store_tbl WHERE store_status = 'Process' ORDER BY store_name ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getRejectedStores()
	{
		$dbHandle = $this->init();
		$query= "Select * from dpc_store_tbl WHERE store_status = 'Rejected' ORDER BY store_name ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function addCategory($post){
		$dbHandle = $this->init();
		if ($post['files']['category_image']['name']!=''){
			$pht = $post['files']['category_image']['name'];
			$filename = "/public/images/all/200x110/".$pht;
			$filetemp = $post['files']['category_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		}else {
			$filename='/public/images/category.png';
		}
		$query = "insert into dpc_category_tbl(category_name, category_url,category_status, category_description, metatitle, metakeyword, metadescription, category_parent, category_image) values('".addslashes($post['category_name'])."','".addslashes($post['category_url'])."','".addslashes($post['category_status'])."','".addslashes($post['category_description'])."', '".addslashes($post['metatitle'])."', '".addslashes($post['metakeyword'])."', '".addslashes($post['metadesc'])."', '".addslashes($post['category_parent'])."', '".$filename."') ";
		$result = $dbHandle->query($query);
	}
	
	public function getAllCategories(){
		$dbHandle = $this->init();
		$query = "select * from dpc_category_tbl order by category_name asc";
		$result = mysql_query($query);
		$categoriesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$categoriesArray[$i] = $row;
			$i++;
		}
		return $categoriesArray;
	}
	
	public function updateCategory($post){
		$dbHandle = $this->init();
		if ($post['files']['category_image']['name']!=''){
			$pht = $post['files']['category_image']['name'];
			$filename = "/public/images/all/200x110/".$pht;
			$filetemp = $post['files']['category_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		}else {
			$filename=$post['category_old_img'];
		}
		$query = "UPDATE dpc_category_tbl SET category_name='".addslashes($post['category_name'])."', category_url='".addslashes($post['category_url'])."', category_status='".addslashes($post['category_status'])."', category_description='".addslashes($post['category_description'])."', metatitle='".addslashes($post['metatitle'])."',metakeyword='".addslashes($post['metakeyword'])."',metadescription='".addslashes($post['metadesc'])."', category_parent='".addslashes($post['category_parent'])."', category_image='".addslashes($filename)."' WHERE category_id='".addslashes($post['category_id'])."'";
		$result = $dbHandle->query($query);
	}
	
	public function getCategoryDetails($category_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_category_tbl where category_id=$category_id";
		$result = mysql_query($query);
		$categoryArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$categoryArray[$i] = $row;
			$i++;
		}
		return $categoryArray;
	}
	
	public function addStore($post){
		$dbHandle = $this->init();
		if(isset($post['files']['store_image']['name']) && $post['files']['store_image']['name']!='' && $post['files']['store_image']['tmp_name']!=''){
			$pht = $post['files']['store_image']['name'];
			error_log("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddinsert user ".print_r($pht));
			$filename = "/public/images/all/200x110/".$pht;
			$filetemp = $post['files']['store_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
		$query = "insert into dpc_store_tbl(store_cat_id, store_name, store_status, store_type, store_image, store_url, store_affilateurl, store_redirecturl, store_utmsource, store_description, metatitle, metakeyword, metadescription) values('".addslashes($post['relevant_category_id'])."' , '".addslashes($post['store_name'])."', 'Process', '".addslashes($post['store_type'])."', '".$filename."', '".addslashes($post['store_url'])."', '".addslashes($post['store_affilateurl'])."', '".addslashes($post['store_redirecturl'])."', '".addslashes($post['store_utmsource'])."','".addslashes($post['store_description'])."','".addslashes($post['metatitle'])."','".addslashes($post['metakeyword'])."','".addslashes($post['metadescription'])."')";
		error_log(" add store **** model ".$query);
		$result = $dbHandle->query($query);
		$store_id=$dbHandle->insert_id();
		foreach($post['store_category_tbl_id'] as $temp){
			$query="insert into dpc_category_store_tbl(category_id, store_id) values('$temp', $store_id)";
			error_log(" add store ****<<<<<<<<<<<<<<<<<<<<<<<<<<<<< model ".$query);
			$result = $dbHandle->query($query);
		}
		$tags = explode(", ",$post['store_tags']);
		foreach($tags as $tag)
		{
			$query = "insert into dpc_tags_tbl(store_id, store_tag) values ('".addslashes($store_id)."', '".addslashes($tag)."')";
			$result = $dbHandle->query($query);
		}
		return $store_id;
	}
	
	public function getStoreDetails($store_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_store_tbl where store_id=$store_id";
		$result = mysql_query($query);
		$storesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$storesArray[$i] = $row;
			$i++;
		}
		return $storesArray;
	}
	
	public function getTagCloud($store_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_tags_tbl where store_id=$store_id";
		$result = mysql_query($query);
		$storesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			array_push($storesArray, $row['store_tag']);
			$i++;
		}
		return $storesArray;
	}
	
	public function getStoreCategories($store_id){
		$dbHandle = $this->init();
		$query = "SELECT cs.category_id FROM dpc_category_store_tbl cs, dpc_category_tbl c where cs.category_id=c.category_id and cs.store_id='$store_id'";
		$result = mysql_query($query);
		$storeCategoriesArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			array_push($storeCategoriesArray, $row['category_id']);
			error_log("storeCtaegoriesArray is ".print_r($storeCategoriesArray, true));
			$i++;
		}
		return $storeCategoriesArray;
	}
	
	public function updateStore($post){
		error_log("update store ?????????????????????? model ".print_r($post, true));
		$dbHandle = $this->init();
		if ($post['files']['store_image']['name']!=''){
			$pht = $post['files']['store_image']['name'];
			$filename = "/public/images/all/200x110/".$pht;
			$filetemp = $post['files']['store_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		}else {
			$filename=$post['store_old_img'];
		}
		error_log("update store *****, model");
		$query = "UPDATE dpc_store_tbl SET store_cat_id='".addslashes($post['relevant_category_id'])."', store_name='".addslashes($post['store_name'])."', store_status='Process', store_type='".addslashes($post['store_type'])."', store_image='".$filename."', store_url='".addslashes($post['store_url'])."', store_description='".addslashes($post['store_description'])."', store_redirecturl='".addslashes($post['store_redirecturl'])."', store_utmsource='".addslashes($post['store_utmsource'])."',metatitle='".addslashes($post['metatitle'])."', metakeyword='".addslashes($post['metakeyword'])."', metadescription='".addslashes($post['metadescription'])."', store_affilateurl='".addslashes($post['store_affilateurl'])."' WHERE store_id='".addslashes($post['store_id'])."'";
		$result = $dbHandle->query($query);
		$query = "delete from dpc_category_store_tbl where store_id='".$post['store_id']."'";
		$result = $dbHandle->query($query);
		$query = "delete from dpc_tags_tbl where store_id='".$post['store_id']."'";
		$result = $dbHandle->query($query);
	
		foreach($post['store_category_tbl_id'] as $temp){
			$query="insert into dpc_category_store_tbl(category_id, store_id) values('$temp', '".$post['store_id']."')";
			error_log(" add store **** model ".$query);
			$result = $dbHandle->query($query);
		}
		$tags = explode(", ",$post['store_tags']);
		foreach($tags as $tag)
		{
			$query = "insert into dpc_tags_tbl(store_id, store_tag) values ('".$post['store_id']."', '".addslashes($tag)."')";
			$result = $dbHandle->query($query);
		}
	}
	
	public function addCoupon($post){
		//error_log("add code XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX**** model ".print_r($post,true));
	
		$dbHandle = $this->init();
		$dlink=$post['coupon_affilateurl'];
	
		if(isset($post['files']['coupon_image']['name']) && $post['files']['coupon_image']['name']!='' && $post['files']['coupon_image']['tmp_name']!=''){
			$pht = $post['files']['coupon_image']['name'];
			$filename = "/public/images/all/200x110/".$pht;
			$filetemp = $post['files']['coupon_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = "";
		}
	
		$query = "insert into dpc_coupons_tbl(`coupon_name`, `coupon_image`, `coupon_description`, `coupon_code`, `coupon_type`, `coupon_off_in`, `coupon_off_by`, `coupon_category`, `coupon_userid`, `coupon_affilateurl`, `coupon_status`, `coupon_startdate`, `coupon_expiredate`,`coupon_offer_only`) values
		('".addslashes($post['coupon_name'])."', '".addslashes($filename)."', '".addslashes($post['coupon_description'])."', '".addslashes($post['coupon_code'])."', '".addslashes($post['coupon_type'])."', '".addslashes($post['coupon_offin'])."', '".addslashes($post['coupon_offby'])."', '".addslashes($post['coupon_cat'])."', '".addslashes($post['user_id'])."', '".addslashes($post['coupon_affilateurl'])."', 'Process', '".addslashes($post['coupon_startdate'])."', '".addslashes($post['coupon_expiredate'])."', '".addslashes($post['coupon_offer_only'])."')";
		error_log("nitesh-???????????????????--------------------bat".$query);
		$result = $dbHandle->query($query);
		$coupon_id=$dbHandle->insert_id();
		foreach($post['code_store'] as $temp){
			$query="insert into dpc_store_coupon_tbl(store_id, coupon_id) values('$temp', $coupon_id)";
			//error_log(" add coupon *???????????????????????????????*** model ".$query);
			$result = $dbHandle->query($query);
		}
	}
	
	public function updateCoupon($post){
		error_log("add code **** model ".print_r($post,true));
		//$date=$post['coupon_expiredate'];
		//$post['coupon_expiredate']=substr($date, -4)."-".substr($date, 0, strlen($date)-5);
		$dbHandle = $this->init();
		$dlink = $dlink=$post['coupon_affilateurl'];
		if(isset($post['files']['coupon_image']['name']) && $post['files']['coupon_image']['name']!='' && $post['files']['coupon_image']['tmp_name']!=''){
			$pht = $post['files']['coupon_image']['name'];
			$filename = "/public/images/all/200x110/".$pht;
			$filetemp = $post['files']['coupon_image']['tmp_name'];
			$this->uploadProductPhotoCode($filename, $filetemp);
		} else {
			$filename = $post['coupon_old_img'];
		}
		$query = "UPDATE dpc_coupons_tbl SET `coupon_name`='".addslashes($post['coupon_name'])."',`coupon_image`='".addslashes($filename)."',`coupon_description`='".addslashes($post['coupon_description'])."',`coupon_code`='".addslashes($post['coupon_code'])."',`coupon_type`='".addslashes($post['coupon_type'])."',`coupon_off_in`='".addslashes($post['coupon_offin'])."',`coupon_off_by`='".addslashes($post['coupon_offby'])."',`coupon_category`='".addslashes($post['coupon_cat'])."',`coupon_userid`='".addslashes($post['user_id'])."',`coupon_affilateurl`='".addslashes($post['coupon_affilateurl'])."',`coupon_status`='Process',`coupon_startdate`='".addslashes($post['coupon_startdate'])."',`coupon_expiredate`='".addslashes($post['coupon_expiredate'])."',`coupon_offer_only`='".addslashes($post['coupon_offer_only'])."' WHERE coupon_id='".addslashes($post['coupon_id'])."'";
		error_log('error msg.................   '.$query);
		$result = $dbHandle->query($query);
		$query ="delete from dpc_store_coupon_tbl where coupon_id='".$post['coupon_id']."'";
		$result = $dbHandle->query($query);
		foreach($post['code_store'] as $temp){
			$query="insert into dpc_store_coupon_tbl(store_id, coupon_id) values('$temp', '".$post['coupon_id']."')";
			$result = $dbHandle->query($query);
		}
	}
	
	public function rejectStore($post){
		$dbHandle = $this->init();
		$query = "update dpc_store_tbl set store_status='Rejected' where store_id='".$post['store_id']."'";
		$result = mysql_query($query);
	}
	
	public function vefifyStore($post){
		$dbHandle = $this->init();
		$query = "update dpc_store_tbl set store_status='Yes' where store_id='".$post['store_id']."'";
		$result = mysql_query($query);
	}
	
	public function getCouponDetails($coupon_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_coupons_tbl cc, dpc_store_tbl cs, dpc_store_coupon_tbl csc where cc.coupon_id='$coupon_id' and cc.coupon_id=csc.coupon_id and cs.store_id=csc.store_id";
		//error_log('checking coupons------- '.$query);
		$result = mysql_query($query);
		$couponArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponArray[$i] = $row;
			$i++;
		}
		return $couponArray;
	}
	
	public function getAllCoupons(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_coupons_tbl ORDER BY coupon_name";
		$result = mysql_query($query);
		$couponArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponArray[$i] = $row;
			$i++;
		}
		return $couponArray;
	}
	
	function bannerUploader($filename, $filetemp){
		$uploadfile = '';
		if(isset($filetemp) && !empty($filetemp)) {
			$baseName = basename($filename);
			$uploadfile = 'public/images/'. $baseName;
			error_log($filetemp.">>>>>>>>>>>>>".$filename);
			move_uploaded_file($filetemp, $uploadfile);
		}
		return;
	}
	
	public function addBanner($post){
		error_log("add code **** model ".print_r($post,true));
		$dbHandle = $this->init();
		if(isset($post['files']['banner_image_path']['name']) && $post['files']['banner_image_path']['name']!='' && $post['files']['banner_image_path']['tmp_name']!=''){
			$pht = $post['files']['banner_image_path']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['banner_image_path']['tmp_name'];
			$this->bannerUploader($filename, $filetemp);
		} else {
			$filename = "";
		}
		if(!empty($post['banner_image'])){
			$filename = $post['banner_image'];
		}
		$query = "insert into dpc_banners_tbl(banner_alt, banner_image, banner_link, banner_store_id, banner_status, banner_category) values('".addslashes($post['banner_alt'])."' , '".addslashes($filename)."' , '".addslashes($post['banner_link'])."' ,  '".$post['banner_store_id']."', '".addslashes($post['banner_status'])."', '".$post['banner_cat_id']."')";
		error_log("nitesh---------------------bat".$query);
		$result = $dbHandle->query($query);
	}
	
	public function updateBanner($post){
		error_log("add code **** model ".print_r($post,true));
		//$date=$post['coupon_expiredate'];
		//$post['coupon_expiredate']=substr($date, -4)."-".substr($date, 0, strlen($date)-5);
		$dbHandle = $this->init();
		if(isset($post['files']['banner_image_path']['name']) && $post['files']['banner_image_path']['name']!='' && $post['files']['banner_image_path']['tmp_name']!=''){
			$pht = $post['files']['banner_image_path']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['banner_image_path']['tmp_name'];
			$this->bannerUploader($filename, $filetemp);
		} else {
			$filename = $post['banner_image'];
		}
		$query = "UPDATE dpc_banners_tbl SET banner_alt='".addslashes($post['banner_alt'])."', banner_image='".addslashes($filename)."', banner_link='".addslashes($post['banner_link'])."', banner_store_id='".$post['banner_store_id']."', banner_status='".addslashes($post['banner_status'])."', banner_category='".addslashes($post['banner_cat_id'])."' WHERE banner_id='".addslashes($post['banner_id'])."'";
		$result = $dbHandle->query($query);
	}
	
	public function getBannerDetails($banner_id){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_banners_tbl where banner_id=$banner_id";
		$result = mysql_query($query);
		$bannerArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$bannerArray[$i] = $row;
			$i++;
		}
		return $bannerArray;
	}
	
	public function detailpage($post){
		error_log("add code ********************************** model ".print_r($post,true));
		$dbHandle = $this->init();
		if(isset($post['files']['coupon_banner']['name']) && $post['files']['coupon_banner']['name']!='' && $post['files']['coupon_banner']['tmp_name']!=''){
			$pht = $post['files']['coupon_banner']['name'];
			$filename = "/public/images/all/650x260/".$pht;
			$filetemp = $post['files']['coupon_banner']['tmp_name'];
			$this->uploadDealPhoto($filename, $filetemp);
		} else {
			$filename = "";
		}
		$query = "INSERT INTO `dpc_deals_tbl`(`deal_name`, `deal_banner`, `deal_description`, `deal_affilateurl`, `deal_status`, `deal_store_id`, `deal_startdate`, `deal_expiredate`) values ('".addslashes($post['coupon_name'])."' , '".addslashes($filename)."' , '".addslashes($post['coupon_description'])."' , '".addslashes($post['coupon_affurl'])."' , '".addslashes($post['coupon_status'])."' , '".addslashes($post['code_store'])."', '".addslashes($post['coupon_startdate'])."' , '".addslashes($post['coupon_expiredate'])."')";
		error_log("nitesh---------------------bat".$query);
		$result = $dbHandle->query($query);
		$coupon_id=$dbHandle->insert_id();
	}
	
	function uploadDealPhoto($filename, $filetemp){
		$uploadfile = '';
		if(isset($filetemp) && !empty($filetemp)) {
			$baseName = basename($filename);
			$uploadfile = 'public/images/all/650x260/'. $baseName;
			error_log($filetemp.">>>>>>>>>>>>>".$filename);
			move_uploaded_file($filetemp, $uploadfile);
		}
		return;
	}
	
	public function getDeals(){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_deals_tbl order by deal_creationdate desc";
		$result = mysql_query($query);
		$bannerArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$bannerArray[$i] = $row;
			$i++;
		}
		return $bannerArray;
	}
	
	public function getDealById($dealId){
		$dbHandle = $this->init();
		$query = "SELECT * FROM dpc_deals_tbl where deal_id='$dealId'";
		$result = mysql_query($query);
		$bannerArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$bannerArray[$i] = $row;
			$i++;
		}
		return $bannerArray;
	}
	
	public function updateDeal($post){
		error_log("add code ********************************** model ".print_r($post,true));
		$dbHandle = $this->init();
		if(isset($post['files']['coupon_banner']['name']) && $post['files']['coupon_banner']['name']!='' && $post['files']['coupon_banner']['tmp_name']!=''){
			$pht = $post['files']['coupon_banner']['name'];
			$filename = "/public/images/all/650x260/".$pht;
			$filetemp = $post['files']['coupon_banner']['tmp_name'];
			$this->uploadDealPhoto($filename, $filetemp);
		} else {
			$filename = $post['deal_banner'];
		}
		$query = "UPDATE `dpc_deals_tbl` SET `deal_name`='".$post['coupon_name']."',`deal_banner`='".$filename."',`deal_description`='".$post['coupon_description']."',`deal_affilateurl`='".$post['coupon_affurl']."',`deal_status`='".$post['coupon_status']."',`deal_store_id`='".$post['code_store']."',`deal_startdate`='".$post['coupon_startdate']."',`deal_expiredate`='".$post['coupon_expiredate']."' WHERE deal_id='".$post['deal_id']."'";
		error_log("nitesh---------------------bat".$query);
		$result = $dbHandle->query($query);
		$coupon_id=$dbHandle->insert_id();
	}
	
	public function getReviewCoupons(){
		$dbHandle = $this->init();
		$date=date('Y-m-d');
		$query = "select c.*, sc.*, s.*, datediff(date(c.coupon_expiredate),curdate()) as diff, cut.* from dpc_coupons_tbl c, dpc_store_tbl s, dpc_store_coupon_tbl sc, dpc_user_tbl cut where c.coupon_id=sc.coupon_id and s.store_id=sc.store_id and c.coupon_expiredate >=  '$date' and c.coupon_status='Process' and c.coupon_userid=cut.user_id order by s.store_name";
		$result = mysql_query($query);
		$couponArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponArray[$i] = $row;
			$i++;
		}
		return $couponArray;
	}
	
	public function verifyCoupon($cId){
		$dbHandle = $this->init();
		$query = "update dpc_coupons_tbl set coupon_status='Yes' where coupon_id='$cId'";
		error_log('kjcbhkdjcdsjcb    '.$query);
		$result = mysql_query($query);
		$couponArray = array();
	}
	
	public function declineCoupon($cId){
		$dbHandle = $this->init();
		$query = "update dpc_coupons_tbl set coupon_status='No' where coupon_id='$cId'";
		error_log('kjcbhkdjcdsjcb    '.$query);
		$result = mysql_query($query);
		$couponArray = array();
	}
	
	public function rejectCoupon($cId){
		$dbHandle = $this->init();
		$query = "update dpc_coupons_tbl set coupon_status='Rejected' where coupon_id='$cId'";
		error_log('kjcbhkdjcdsjcb    '.$query);
		$result = mysql_query($query);
		$couponArray = array();
	}
	
	public function getStoreCoupons($cpnBy,$storeId,$userId){
		$dbHandle = $this->init();
		$date=date('Y-m-d');
		if($cpnBy=='rejected'){
			$query = "select c.*, sc.*, s.*, datediff(date(c.coupon_expiredate),curdate()) as diff from dpc_coupons_tbl c, dpc_store_tbl s, dpc_store_coupon_tbl sc where c.coupon_id=sc.coupon_id and s.store_id=sc.store_id and c.coupon_status='Rejected' and c.coupon_userid='$userId' and c.coupon_expiredate >=  '$date' order by c.coupon_creationdate desc";
		}
		if($cpnBy=='store'){
			$query = "select c.*, sc.*, s.*, datediff(date(c.coupon_expiredate),curdate()) as diff from dpc_coupons_tbl c, dpc_store_tbl s, dpc_store_coupon_tbl sc where c.coupon_id=sc.coupon_id and s.store_id=sc.store_id and sc.store_id='$storeId' and c.coupon_expiredate >=  '$date' and c.coupon_status in('Yes','Process') order by c.coupon_creationdate desc";
		}
		error_log("get store coupons query is ".$query);
		$result = mysql_query($query);
		$couponsArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$couponsArray[$i] = $row;
			$i++;
		}
		return $couponsArray;
	}
	
	public function getAllHomeBanner()
	{
		$dbHandle = $this->init();
		$query= "Select * from dpc_homebanner_tbl ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function getHomeBannerById($bId)
	{
		$dbHandle = $this->init();
		$query= "Select * from dpc_homebanner_tbl where homebanner_id='$bId' ";
		$result = mysql_query($query);
		$resultArray = array();
		$i=0;
		while($row = mysql_fetch_assoc($result)){
			$resultArray[$i] = $row;
			$i++;
		}
		return $resultArray;
	}
	
	public function insertHomeBanner($post)
	{
		error_log("add code **** model ".print_r($post,true));
		$dbHandle = $this->init();
		if(isset($post['files']['banner_image_path']['name']) && $post['files']['banner_image_path']['name']!='' && $post['files']['banner_image_path']['tmp_name']!=''){
			$pht = $post['files']['banner_image_path']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['banner_image_path']['tmp_name'];
			$this->bannerUploader($filename, $filetemp);
		} else {
			$filename = "";
		}
		if(!empty($post['banner_image'])){
			$filename = $post['banner_image'];
		}
		$query = "insert into `dpc_homebanner_tbl`(`homebanner_alt`, `homebanner_image`, `homebanner_link`, `homebanner_status`) values('".addslashes($post['banner_alt'])."' , '".addslashes($filename)."' , '".addslashes($post['banner_link'])."' , '".addslashes($post['banner_status'])."')";
		error_log("nitesh---------------------bat".$query);
		$result = $dbHandle->query($query);
	}
	
	public function updateHomeBanner($post){
		error_log("add code **** model ".print_r($post,true));
		//$date=$post['coupon_expiredate'];
		//$post['coupon_expiredate']=substr($date, -4)."-".substr($date, 0, strlen($date)-5);
		$dbHandle = $this->init();
		if(isset($post['files']['homebanner_image_path']['name']) && $post['files']['homebanner_image_path']['name']!='' && $post['files']['homebanner_image_path']['tmp_name']!=''){
			$pht = $post['files']['homebanner_image_path']['name'];
			$filename = "/public/images/".$pht;
			$filetemp = $post['files']['homebanner_image_path']['tmp_name'];
			$this->bannerUploader($filename, $filetemp);
		} else {
			$filename = $post['homebanner_image'];
		}
		$query = "UPDATE `dpc_homebanner_tbl` SET `homebanner_alt`='".addslashes($post['homebanner_alt'])."',`homebanner_image`='".addslashes($filename)."',`homebanner_link`='".addslashes($post['homebanner_link'])."',`homebanner_status`='".addslashes($post['homebanner_status'])."' WHERE homebanner_id='".addslashes($post['homebanner_id'])."'";
		$result = $dbHandle->query($query);
	}

}